<?php

/*
 * View/PSPlanningView.php
 */

namespace RHX\Model\View;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use RHX\Model\Object\PSPlanning;
use RHX\Model\Object\Periode;

class PSPlanningView extends AbstractTableGateway {

    protected $table = 'rh_view_periode_semaine_planning';

    public function __construct(Adapter $adapter) {
	$this->adapter			 = $adapter;
	$this->resultSetPrototype	 = new ResultSet();
	$this->resultSetPrototype->setArrayObjectPrototype(new PSPlanning($adapter));
	$this->initialize();
    }

    public function getAll() {
	return $this->select();
    }

    public function getAllBySite($siteId = 1) {
	return $this->select(function(Select $select) use($siteId) {
			    $where = new \Zend\Db\Sql\Where();
			    $where->equalTo('site_id', $siteId);
			    $where->or->isNull('site_id');
			    $select->where($where);
			});
    }

    /**
     * Vue Planning Periode Semaine
     * 
     * @param int $periode
     * @param int $semaine
     * @param int $site
     * @return \RHX\Model\Object\PSPlanning
     */
    public function getOne($periode, $semaine, $site) {
	$row = $this->select(function(Select $select) use($periode, $semaine, $site) {
			    $where = new Where();
			    $where->equalTo('site_id', $site);
			    $where->equalTo('periode_id', $periode);
			    $where->equalTo('semaine_id', $semaine);
			    $select->where($where);
			})->current();

	if (!$row) {
	    return new PSPlanning($this->adapter);
	}
	else {
	    return $row;
	}
    }

    public function getByTemplate($template) {
	return $this->select(function(Select $select)use($template) {
			    $where = new Where();
			    $where->equalTo('template', $template);
			    $select->where($where);
			});
    }

    /**
     * Récupérer Le planning Template d'un employé
     *
     * @param int $planningId
     * @param int  $matricule
     * @return \RHX\Model\Table\EmployeTemplateTable
     */
    public function getTemplate($planningId, $matricule) {
	// Table
	$planningSemaineTable	 = new \RHX\Model\Table\PlanningSemaineTable($this->adapter);
	$employeTemplateTable	 = new \RHX\Model\Table\EmployeTemplateTable($this->adapter);

	/**
	 * Planning Semaine en cours
	 * 
	 * @var \RHX\Model\Object\PlanningSemaine
	 */
	$planning	 = $planningSemaineTable->get($planningId);
	$semaine	 = $planning->getSemaine();
	//var_dump($semaine);

	$rows = $this->select(array('semaine_id' => $semaine->id));
	$template	 = null;
	// Template
	foreach ($rows as $row) {
	    $template = $employeTemplateTable->getEmployeTemplate($matricule, $row->template);
//	    echo "<pre>Paramètres : id: $planningId, matricule: $matricule, n° Template : " . $row->template . "</pre>";
//	    var_dump($template);
	    if ($template)
		break;
	}
	if (is_null($template) || !$template) {
	    $template		 = new \RHX\Model\Object\EmployeTemplate($this->getAdapter());
	    $template->matricule	 = $matricule;
	    $template->heure_prevues = '35:00';
	}

	return $template;
    }
    
    /**
     * Récupérer les enregistrements contenant le numéro d'un template utilisé dans la génération d'un planning semaine
     * 
     * @param type $template
     * @return ResultSet
     */
    public function getTemplates($template){
	return $this->select(function(Select $select)use($template){
	    $where = new Where();
	    $where->equalTo('template', $template);
	    $select->where($where);
	});
    }
    
    /**
     * Récupérer la dernière semaine générer pour le site
     * 
     * @param type $siteId
     * @return \RHX\Model\Object\Semaine
     */
    public function getLastWeek($siteId){
	$sql = "SELECT MAX(lundi) as 'max_lundi', semaine_id as 'semaine_id' FROM $this->table WHERE site_id = $siteId";
	$result	 = $this->adapter
		->query($sql)
		->execute();
	$rows = array();
	foreach($result as $row){
	    $rows[] = $row;
	}
	if(count($rows) != 0){
	    $current = null;
	    if(isset($rows[0])){
		$current = $rows[0];
	    }else{
		return null;
	    }
	    $semaineId = (int)$current['semaine_id'];
	    $tableSemaine = new \RHX\Model\Table\SemaineTable($this->getAdapter());
	    try{
		return $tableSemaine->get($semaineId);
	    }catch(\Exception $ex){
		return null;
	    }
	}else{
	    return null;
	}
    }
    
    /**
     * Récupérer les semaines ayants un planning semaine
     * 
     * @param int $periodeId
     * @param int $siteId
     * @return \RHX\Model\Object\Semaine
     */
    public function getWeek($periodeId, $siteId){
	$rows = $this->select(function(Select $select)use($periodeId, $siteId){
	    $where = new Where();
	    $where->equalTo('periode_id', $periodeId);
	    $where->equalTo('site_id', $siteId);
	    $where->isNotNull('template');
	    $select->where($where);
	});
	$semaines = array();
	foreach ($rows as $row){
	    $semaines[] = $row->getSemaine();
	}
	return $semaines;
    }
    
    /**
     * Info Planning
     * 
     * @param int $siteId
     * @param int $weekId
     * @return PSPlanning|NULL
     */
    public function getPlanningByWeek($siteId, $weekId){
	$row = $this->select(function(Select $select)use($siteId, $weekId){
	    $where = new Where();
	    $where->equalTo('site_id', $siteId);
	    $where->equalTo('semaine_id', $weekId);
	    $select->where($where);
	})->current();
	return ($row) ? $row : NULL;
    }

}

?>
